{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('../')\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import steward as st\n",
    "import matplotlib.pyplot as plt\n",
    "import pickle\n",
    "import xgboost\n",
    "from sklearn.metrics import roc_auc_score\n",
    "%matplotlib inline\n",
    "from src import build\n",
    "from src import train, config\n",
    "from src.feature_cols import to_drop\n",
    "import h5py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "build.build_all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "feature_list = [\n",
    "\n",
    "    'basic_preprocess/cont_train_all',\n",
    "    'basic_preprocess/conc_train_all',\n",
    "    \n",
    "    \n",
    "    'feature/rank1_train_all',\n",
    "    'feature/rank2_train_all',\n",
    "    'feature/rank3_train_all',\n",
    "    \n",
    "    'feature/history1_train_all',\n",
    "    'feature/history2_train_all',\n",
    "    'feature/history3_train_all',\n",
    "\n",
    "    'feature/ordnum_train_all',\n",
    "    \n",
    "    'feature/room1_train_all',\n",
    "    'feature/basicroom1_train_all',\n",
    "    'feature/orderroom1_train_all',\n",
    "    \n",
    "    'feature/rank_history1_train_all',\n",
    "    'feature/rank_history2_train_all',\n",
    "    'feature/rank_history3_train_all',\n",
    "\n",
    "]\n",
    "\n",
    "y = 'basic_preprocess/y_train_all'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'config' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-5-3ccc879c790e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mh5py\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mFile\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mconfig\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpj_root\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'data/train.hdf5'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'r'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'config' is not defined"
     ]
    }
   ],
   "source": [
    "f = h5py.File(config.pj_root + 'data/train.hdf5', 'r')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "use_n = 2000000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "__x = []\n",
    "for name in feature_list:\n",
    "    __x.append(f[name][0:use_n])\n",
    "\n",
    "y_df = st.get_instance(y).load()[0:use_n]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = np.concatenate(__x, axis=1)\n",
    "\n",
    "columns = []\n",
    "for name in feature_list:\n",
    "    columns.extend(f[name].attrs['columns'].astype(str))\n",
    "\n",
    "X_df = pd.DataFrame(x, columns=columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "t = st.LoadInstance('test/all_30W')\n",
    "im = t()['im']\n",
    "select_cols = im.head(200).index.tolist()\n",
    "X_df = X_df[select_cols]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model_para = {\n",
    "    'objective': 'rank:pairwise',\n",
    "    'n_estimators': 220,\n",
    "#     'scale_pos_weight': 5,\n",
    "    'learning_rate': 0.3,\n",
    "    'max_depth': 4\n",
    "}\n",
    "\n",
    "train.train_fold(X_df, y_df, model_para=model_para, save=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "XGBClassifier(base_score=0.5, colsample_bylevel=1, colsample_bytree=1,\n",
       "       gamma=0, learning_rate=0.3, max_delta_step=0, max_depth=4,\n",
       "       min_child_weight=1, missing=None, n_estimators=600, nthread=-1,\n",
       "       objective='rank:pairwise', reg_alpha=0, reg_lambda=1,\n",
       "       scale_pos_weight=1, seed=0, silent=True, subsample=1)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model_para = {\n",
    "    'objective': 'rank:pairwise',\n",
    "    'n_estimators': 600,\n",
    "#     'scale_pos_weight': 5,\n",
    "    'learning_rate': 0.3,\n",
    "    'max_depth': 4\n",
    "}\n",
    "train.train_all(X_df, y_df, model_para=model_para)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model_para = {\n",
    "    'objective': 'rank:pairwise',\n",
    "    'n_estimators': 1000,\n",
    "#     'scale_pos_weight': 5,\n",
    "    'learning_rate': 0.3,\n",
    "    'max_depth': 4\n",
    "}\n",
    "r = train.train_xgboost_step(X_df, y_df, early_stopping_rounds=50, n_estimators=1000, model_para=model_para)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model_para = {\n",
    "    'objective': 'rank:pairwise',\n",
    "    'n_estimators': 175,\n",
    "#     'scale_pos_weight': 5,\n",
    "    'learning_rate': 0.3,\n",
    "    'max_depth': 4\n",
    "}\n",
    "r = train.train_all(X_df, y_df, model_para=model_para)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "len(X_df.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model_para_ref = {\n",
    "    'objective': ['rank:pairwise'],\n",
    "    'n_estimators': [200], \n",
    "    'learning_rate': [0.3],\n",
    "    'max_depth': [4],\n",
    "    'min_child_weight':[1]\n",
    "}\n",
    "\n",
    "result = train.feature_importance_xgboost(X_df, y_df, 1, model_para_ref=model_para_ref, use_seed=True, verbose=True)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
